package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.Demand;
import org.springblade.modules.app.service.IDemandService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

////优惠券
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/demand")
public class DemandController extends BladeController {

    private final IDemandService demandService;

    //////优惠券列表
    @GetMapping("/discountList")
    public List<Demand> discountList(Demand demand) {
        BladeUser bladeUser = AuthUtil.getUser();
        LambdaQueryWrapper<Demand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(wrapper -> wrapper.like(Demand::getDelflag, "1"));
        queryWrapper.and(wrapper -> wrapper.like(Demand::getUserid, bladeUser.getUserId()));
        List<Demand> result = demandService.list(queryWrapper);
        return result;
    }


    //////分页
    @GetMapping("/page")
    public R<IPage<Demand>> page(Page page, Demand demand) {
        BladeUser bladeUser = AuthUtil.getUser();

        LambdaQueryWrapper<Demand> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(demand.getTitle())) {
            queryWrapper.and(wrapper -> wrapper.like(Demand::getTitle, demand.getTitle()));
        }
        //queryWrapper.and(wrapper -> wrapper.like(Demand::getUserid, bladeUser.getUserId()));

        IPage<Demand> pages = demandService.page(page, queryWrapper);
        return R.data(pages);
    }


    //////优惠券列表
    @GetMapping("/list")
    public List<Demand> list(Demand demand) {
        BladeUser bladeUser = AuthUtil.getUser();

        LambdaQueryWrapper<Demand> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(demand.getTitle())) {
            queryWrapper.and(wrapper -> wrapper.like(Demand::getTitle, demand.getTitle()));
        }
        //  queryWrapper.and(wrapper -> wrapper.like(Demand::getUserid, bladeUser.getUserId()));
        List<Demand> result = demandService.list(queryWrapper);
        return result;
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入demand")
    public R<Demand> detail(Demand demand) {
        Demand detail = demandService.getOne(Condition.getQueryWrapper(demand));
        return R.data(detail);
    }


    /**
     * 新增 需求发布
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入demand")
    public R save(@RequestBody Demand demand) {
        demand.setCtime(new Date());
        return R.status(demandService.save(demand));
    }

    /**
     * 修改 需求发布
     */
    @PostMapping("/update")
    public R update(@RequestBody Demand demand) {
        return R.status(demandService.updateById(demand));
    }

    /**
     * 新增或修改 需求发布
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入demand")
    public R submit(@RequestBody Demand demand) {
        return R.status(demandService.saveOrUpdate(demand));
    }


    /**
     * 删除 需求发布
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(demandService.removeByIds(Func.toLongList(ids)));
    }


}
